import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ApiUrl} from "../../../config/api-url";
import {BaseService} from "../../../shared/service/base.service";
import {FormBuilder, FormGroup, FormControl, Validators, ValidatorFn, AbstractControl} from '@angular/forms';

function numberValidator(): ValidatorFn {
  return (control: AbstractControl): {[key: string]: any} | null => {
    const forbidden = isNaN(control.value);
    return forbidden ? {forbiddenNumber: {value: control.value}, isNumber: true} : null;
  };
}

@Component({
  selector: 'app-store-quota',
  templateUrl: './store-quota.component.html',
  styleUrls: ['./store-quota.component.less']
})
export class StoreQuotaComponent implements OnInit {
  @Input()
  nzVisible = false;
  @Input()
  store: any;
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  status = false;
  isOkLoading = false;
  merchantStoreForm: FormGroup;

  constructor(private baseService: BaseService, private fb: FormBuilder) {

    this.merchantStoreForm = this.fb.group({
      userId: new FormControl(''),
      id: new FormControl(''),
      dayQuota: new FormControl(''),
      memberMinuteQuota: new FormControl(''),
      memberMinutePayCount: new FormControl(''),
      memberHourQuota: new FormControl(''),
      memberHourPayCount: new FormControl(''),
      memberDayQuota: new FormControl(''),
      memberDayPayCount: new FormControl(''),
    });
  }

  ngOnInit(): void {
    this.merchantStoreForm = this.fb.group({
      dayQuota: ['', [Validators.min(1000), numberValidator()]],
      memberMinuteQuota: ['', [Validators.min(100), numberValidator()]],
      memberMinutePayCount: ['', [Validators.min(1), numberValidator()]],
      memberHourQuota: ['', [Validators.min(300), numberValidator()]],
      memberHourPayCount: ['', [Validators.min(1), numberValidator()]],
      memberDayQuota: ['', [Validators.min(1000), numberValidator()]],
      memberDayPayCount: ['', [Validators.min(1), numberValidator()]]
    });
  }
  // 显示表单控件后触发校验
  onShowForm() {
    this.merchantStoreForm.get('dayQuota').markAsTouched();
    this.merchantStoreForm.get('dayQuota').updateValueAndValidity();
    this.merchantStoreForm.get('memberMinuteQuota').markAsTouched();
    this.merchantStoreForm.get('memberMinuteQuota').updateValueAndValidity();
    this.merchantStoreForm.get('memberMinutePayCount').markAsTouched();
    this.merchantStoreForm.get('memberMinutePayCount').updateValueAndValidity();
    this.merchantStoreForm.get('memberHourQuota').markAsTouched();
    this.merchantStoreForm.get('memberHourQuota').updateValueAndValidity();
    this.merchantStoreForm.get('memberHourPayCount').markAsTouched();
    this.merchantStoreForm.get('memberHourPayCount').updateValueAndValidity();
    this.merchantStoreForm.get('memberDayQuota').markAsTouched();
    this.merchantStoreForm.get('memberDayQuota').updateValueAndValidity();
    this.merchantStoreForm.get('memberDayPayCount').markAsTouched();
    this.merchantStoreForm.get('memberDayPayCount').updateValueAndValidity();
  }
  ngOnChanges() {
    this.status = false;
    this.merchantStoreForm.patchValue({
      dayQuota: this.store.dayQuota
    });
    if (this.store?.memberMinuteQuota != null) {
      this.merchantStoreForm.patchValue({
        memberMinuteQuota: this.store.memberMinuteQuota
      });
    }
    if (this.store?.memberMinutePayCount != null) {
      this.merchantStoreForm.patchValue({
        memberMinutePayCount: this.store.memberMinutePayCount
      });
    }
    if (this.store?.memberHourQuota != null) {
      this.merchantStoreForm.patchValue({
        memberHourQuota: this.store.memberHourQuota
      });
    }
    if (this.store?.memberHourPayCount != null) {
      this.merchantStoreForm.patchValue({
        memberHourPayCount: this.store.memberHourPayCount
      });
    }
    if (this.store?.memberDayQuota != null) {
      this.merchantStoreForm.patchValue({
        memberDayQuota: this.store.memberDayQuota
      });
    }
    if (this.store?.memberDayPayCount != null) {
      this.merchantStoreForm.patchValue({
        memberDayPayCount: this.store.memberDayPayCount
      });
    }
  }
  // 取消
  handleCancel() {
    this.nzVisible = false;
    this.merchantStoreForm?.reset({});
    this.emit.emit(this.nzVisible);
  }
  //修改
  modification(){
    this.onShowForm();
    this.status = true;
  }

  handleOk() {
    if (!this.merchantStoreForm.valid) {
        this.baseService.layer.messageError('请根据红色提示修改对应数据');
        return;
    }
    this.isOkLoading = true;
    this.merchantStoreForm.value.id = this.store.id;
    this.merchantStoreForm.value.userId = this.store.userId;
    this.baseService.put(ApiUrl.MerchantStoreUrl, this.merchantStoreForm.value).subscribe(data => {
      if (data.status == 0) {
        this.baseService.layer.messageSuccess('修改成功');
        this.handleCancel();
        this.isOkLoading = false;
      } else {
        this.baseService.layer.messageError(data.msg);
        this.isOkLoading = false;
      }
    });

  }
}
